package controllers;

import com.google.gson.JsonObject;
import constants.TplPath;
import constants.system.MemberShip;
import constants.ws.PageBean;
import duppentity.dto.recharge.BuRechargeCouponDto;
import duppentity.dto.recharge.BuRechargeCouponDtoUtil;
import duppentity.dto.recharge.BuRechargeDto;
import duppentity.dto.recharge.BuRechargeDtoUtil;
import utils.RequestHelper;
import utils.Utils;

import java.util.List;

/**
 * Description:  充值管理
 * Author: pengpeng@djcars.cn
 * Date: 2019/12/5 16:10
 */

public class RechargeController extends ApiController{

    /**
     * 充值列表
     */
    public static void rechargeList(){
        String page = TplPath.rechargeList;
        if (Utils.exitFile(page)){
            renderTemplate(page);
        }
    }

    public static void getRechargeList() {
        PageBean pageBean = PageBean.get();
        String orderBy = "bu_recharge.order_index asc,bu_recharge.create_time desc";

        JsonObject data = rechargeDupp.getRechargeList(pageBean.getOffset(), pageBean.pageCount(), orderBy);
        if (!checkResult(data)) {
            apiError(data);
        }
        List<BuRechargeDto> buRechargeList = BuRechargeDtoUtil.getBuRechargeDtoList(data);
        apiList(data, buRechargeList);
    }

    public static void addRecharge() {
        String page = TplPath.addRecharge;
        if (Utils.exitFile(page)) {
            renderTemplate(page);
        }
    }

    public static void saveRecharge() {
        Integer orderIndex = params.get("orderIndex", Integer.class);
        Long id = RequestHelper.getLongParam("id");

        if (id > 0) {
            JsonObject data = rechargeDupp.updateRecharge(id, orderIndex, MemberShip.current().getId());
            if (checkResult(data)) {
                if (data.get("result").getAsBoolean()) {
                    apiOk("修改权重成功");
                } else {
                    apiError("修改权重失败");
                }
            } else {
                apiError(data);
            }
        } else {
            Integer amount = Utils.getIntPrice(params.get("amount", Double.class));
            JsonObject data = rechargeDupp.saveRecharge(amount, orderIndex, MemberShip.current().getId());
            if (checkResult(data)) {
                if (data.get("id").getAsInt() > 0) {
                    apiOk("新增充值类型成功");
                } else {
                    apiError("新增充值类型成功");
                }
            } else {
                apiError(data);
            }
        }
    }

    public static void updateInValid() {
        Long id = params.get("id", Long.class);
        Integer invalid = params.get("invalid", Integer.class);

        JsonObject data = rechargeDupp.updateInValid(id, invalid, MemberShip.current().getId());
        if (checkResult(data)) {
            if (data.get("result").getAsBoolean()) {
                apiOk("修改展示状态成功");
            } else {
                apiError("修改展示状态失败");
            }
        } else {
            apiError(data);
        }
    }

    /**
     * 充值的福利配置
     */
    public static void rechargeCouponList() {
        Long rechargeId = params.get("rechargeId", Long.class);
        String page = TplPath.rechargeCouponList;
        if (Utils.exitFile(page)) {
            renderTemplate(page, rechargeId);
        }
    }

    public static void getRechargeCouponList() {
        PageBean pageBean = PageBean.get();
        Long rechargeId = params.get("rechargeId", Long.class);

        JsonObject data = rechargeDupp.getRechargeCouponList(rechargeId, pageBean.getOffset(), pageBean.pageCount());
        if (!checkResult(data)) {
            apiError(data);
        }
        List<BuRechargeCouponDto> buRechargeCouponList = BuRechargeCouponDtoUtil.getBuRechargeCouponDtoList(data);
        apiList(data, buRechargeCouponList);
    }

    public static void addRechargeCoupon() {
        Long rechargeId = params.get("rechargeId", Long.class);
        JsonObject couponList = couponDupp.getCouponList(null, null, null, 1, 0, Integer.MAX_VALUE);

        String page = TplPath.addRechargeCoupon;
        if (Utils.exitFile(page)) {
            renderTemplate(page, couponList, rechargeId);
        }
    }

    public static void saveRechargeCoupon() {
        Long rechargeId = params.get("rechargeId", Long.class);
        Long couponId = params.get("couponId", Long.class);
        Integer num = params.get("num", Integer.class);

        JsonObject data = rechargeDupp.saveRechargeCoupon(rechargeId, couponId, num, 0, MemberShip.current().getId());
        if (checkResult(data)) {
            if (data.get("id").getAsInt() > 0) {
                apiOk("新增福利配置成功");
            } else {
                apiError("新增福利配置失败");
            }
        } else {
            apiError(data);
        }
    }

    public static void deleteRechargeCoupon() {
        Long id = params.get("id", Long.class);

        JsonObject data = rechargeDupp.deleteRechargeCoupon(id, MemberShip.current().getId());
        if (checkResult(data)) {
            if (data.get("result").getAsBoolean()) {
                apiOk("删除福利配置成功");
            } else {
                apiError("删除福利配置失败");
            }
        } else {
            apiError(data);
        }
    }
}

